﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Services;

namespace Catering.Catering_Page.system
{
    public partial class RecipesRuleManageInfo : UserIsNull
    {
        public string m_DishTypesList = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            m_DishTypesList = InitDishTypes();
        }

        public string InitDishTypes()
        {
            StringBuilder result = new StringBuilder("");
            DataTable dt = new Catering_Rules.DishType().GetAllList().Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                Dictionary<int, string> dic = new Catering_Rules.RecipesRuleManage().GetRuleTypelist();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int id = Convert.ToInt16(dt.Rows[i]["id"]);
                    string dtname = dt.Rows[i]["DT"].ToString();
                    result.Append("<div class='tp_ui'>");
                    result.Append("<span class='tp_t'> " + dtname + "　》</span>");
                    result.Append("<dl class='dps_ui'>");
                    DataTable dt_info = new Catering_Rules.DishTypes().GetList(" DTID=" + id).Tables[0];
                    if (dt_info != null && dt_info.Rows.Count > 0)
                    {
                        for (int k = 0; k < dt_info.Rows.Count; k++)
                        {
                            int dtsid = Convert.ToInt16(dt_info.Rows[k]["id"]);
                            string dtsname = dt_info.Rows[k]["DTName"].ToString();
                            int df_rule = 1;
                            int df_no = 0;
                            Catering_Model.RecipesRuleManage rm = new Catering_Rules.RecipesRuleManage().GetModel(dtsid);
                            if (rm != null)
                            {
                                df_rule = rm.Repeatype;
                                df_no = rm.DefaultNo;
                            }
                            string options = GetOptions(dic, df_rule);
                            result.Append("<dd><span>" + dtsname + "：</span><select typesid='" + dtsid + "'>" + options + "</select><input type='text' ntypesid='" + dtsid + "' value='" + df_no + "' style='width:20px;' /></dd>");
                        }
                        dt_info.Dispose();
                    }
                    result.Append("</dl>");
                    result.Append("<div class='clear'></div>");
                    result.Append("</div>");
                }
                dt.Dispose();
            }
            return result.ToString();
        }

        /// <summary>
        /// 获取标准列表信息
        /// </summary>
        /// <returns></returns>
        public string GetOptions(Dictionary<int, string> dic, int selectvalue)
        {
            StringBuilder result = new StringBuilder("");

            foreach (int key in dic.Keys)
            {
                if (key == selectvalue)
                    result.Append("<option value='" + key + "' selected='selected'>" + dic[key] + "</option>");
                else
                    result.Append("<option value='" + key + "'>" + dic[key] + "</option>");
            }
            return result.ToString();
        }

        [WebMethod]
        public static string SaveList(string str)
        {
            if (string.IsNullOrEmpty(str)) return "";
            string[] ui = str.Split('|');
            if (ui.Length == 0) return "";
            foreach (string i in ui)
            {
                if (string.IsNullOrEmpty(i)) continue;
                string[] op = i.Split(',');
                if (op.Length != 3) continue;
                int id = Convert.ToInt32(op[0]);
                int repeattype = Convert.ToInt32(op[1]);
                int no = Convert.ToInt32(op[2]);
                Catering_Model.RecipesRuleManage rm = new Catering_Rules.RecipesRuleManage().GetModel(id);
                if (rm != null)
                {
                    rm.Repeatype = repeattype;
                    rm.DefaultNo = no;
                    new Catering_Rules.RecipesRuleManage().Update(rm);
                }
                else
                {
                    rm = new Catering_Model.RecipesRuleManage();
                    rm.DTID = id;
                    rm.Repeatype = repeattype;
                    rm.DefaultNo = no;
                    new Catering_Rules.RecipesRuleManage().Add(rm);
                }
            }
            return "";
        }
    }
}